const {ipcRenderer} = require("electron");
const {request} = require("../components/request.js");

document.addEventListener('DOMContentLoaded', () => {

    // 对应ID获取
    const CPU_model = document.getElementById("CPU_model");
    const GPU_model = document.getElementById("GPU_model");
    const DDR_model = document.getElementById("DDR_model");
    const HDD_model = document.getElementById("HDD_model");
    const Motherboard_model = document.getElementById("Motherboard_model");
    const Mac_model = document.getElementById("Mac_model");
    const systemID = document.getElementById("systemID");
    const buttonText = document.getElementById("buttonText");
    const illustrate = document.getElementById("illustrate");

    // 触发请求
    ipcRenderer.send('SendDate', null);

    // 监听响应
    ipcRenderer.on('SendDate', (event, info, success) => {
        console.log("Received SendDate event", info, success);

        // 更新对应的 DOM 元素
        if (success) {
            CPU_model.textContent = info.cpu;
            GPU_model.textContent = info.gpu;
            DDR_model.textContent = info.memory;
            HDD_model.textContent = info.storage;
            Motherboard_model.textContent = info.motherboard;
            Mac_model.textContent = info.macAddress;
            systemID.textContent = "唯一码: " + info.machineIDSync;

            // 向服务端上传数据
            request({
                url: 'addUser',
                params: {
                    passCode: "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",//密钥(必须保证与服务端密钥一致性)
                    machineID: info.machineIDSync,
                    cpu: info.cpu,
                    gpu: info.gpu,
                    memory: info.memory,
                    storage: info.storage,
                    motherboard: info.motherboard,
                    macAddress: info.macAddress,
                }
            }).then(res => {
                if (res.data.success) { // 确保检查正确的属性
                    buttonText.textContent = res.data.message;
                    buttonText.style.color = "rgb(26, 235, 18)";
                } else {
                    buttonText.textContent = res.data.message;
                    buttonText.style.color = "#f00";
                }
            }).catch(err => {
                buttonText.textContent = "网络错误,无法与服务器获得连接";
                buttonText.style.color = "#f00";
            });
        } else {
            buttonText.textContent = "无法获得硬件数据";
            buttonText.style.color = "#f00";
        }
    });
    illustrate.addEventListener("click", function () {
        alert(
            "       计算机硬件信息收集客户端是一款用于收集和展示计算机系统硬件信息的桌面应用程序。\n" +
            "该软件整理 CPU、GPU、内存、存储、主板和 MAC 地址等硬件信息，并将这些数据上传到指定服务器。\n" +
            "       该软件不存在任何监控行为。\n" +
            "       开源仓库：https://gitee.com/zhouzhangjian/HICS_CS"
        );
    })
});